package cn.cosmosx.controller.sys;

import cn.cosmosx.base.generator.excel.AutoExcelCodeGenerator;
import cn.cosmosx.base.generator.excel.param.EntityModelParam;
import cn.cosmosx.base.generator.mp.AutoCodeGenerator;
import cn.cosmosx.controller.sys.param.GeneratorParam;
import freemarker.template.TemplateException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(tags = "代码生成")
@RestController
@RequestMapping(value = "/sys/generate")
public class GeneratorController {
    @Autowired
    private AutoCodeGenerator autoCodeGenerator;
    @Autowired
    private AutoExcelCodeGenerator autoExcelCodeGenerator;

    @ApiOperation(value = "代码生成（基于Excel）")
    @PostMapping(value = "/excel")
    public String download(HttpServletResponse response,
                        @RequestPart MultipartFile excelFile,
                        EntityModelParam param) throws IOException, TemplateException {
        autoExcelCodeGenerator.generate(excelFile.getInputStream(), param);
        return "生成成功";
    }

    @ApiOperation(value = "代码生成（基于MP）", notes = "需要生成代码的表（传入表名，若多张表，则以“,”分隔;若生成全部，则传入“all”）")
    @PostMapping("/mp")
    public Object generate(@RequestBody GeneratorParam param) {
        autoCodeGenerator.generate(param);
        return "生成成功";
    }

}
